package org.jeecg.common.modules.doctor.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.entity.DoctorCertification;
import org.jeecg.common.modules.doctor.mapper.DoctorCertificationMapper;
import org.jeecg.common.modules.doctor.service.IDoctorCertificationService;
import org.jeecg.common.modules.doctor.service.IDoctorService;
import org.jeecg.common.util.DySmsEnum;
import org.jeecg.common.util.DySmsHelper;
import org.jeecg.common.util.enums.CertificationStatusEnum;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @Description: doctor_certification
 * @Author: jeecg-boot
 * @Date:   2020-08-15
 * @Version: V1.0
 */
@Service
public class DoctorCertificationServiceImpl extends ServiceImpl<DoctorCertificationMapper, DoctorCertification> implements IDoctorCertificationService {

    @Resource
    private IDoctorService doctorService;

    @Override
    public DoctorCertification getByDoctorId(String doctorId) {
        LambdaQueryWrapper<DoctorCertification> query = Wrappers.lambdaQuery();
        query.eq(DoctorCertification::getDoctorId,doctorId);
        return getOne(query);
    }


    @Override
    public void audit(String id,String loginUser,Integer status){
        CertificationStatusEnum statusEnum;
        if (status == 1){
            statusEnum = CertificationStatusEnum.CERTIFICATION;
        }else if (status == 2){
            statusEnum = CertificationStatusEnum.FAIL;
        }else{
            return;
        }
        //1 成功 2失败
        DoctorCertification doctorCertification = getById(id);
        doctorCertification.setCheckTime(new Date());
        doctorCertification.setCheckUser(loginUser);
        updateById(doctorCertification);
        doctorService.updateDoctorCertificationStatus(doctorCertification.getDoctorId(),statusEnum,loginUser);
        try {
            Doctor doctor = doctorService.getById(doctorCertification.getDoctorId());
            DySmsHelper.sendSms(doctor.getMobile(),new JSONObject(), Objects.equals(statusEnum,CertificationStatusEnum.CERTIFICATION) ?  DySmsEnum.DOCTOR_CERT_SUCCESS :  DySmsEnum.DOCTOR_CERT_FAIL);
        } catch (Exception e) {
            log.error("短信发送失败",e);
        }

    }
}
